﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class Bookmarks : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // security part
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            OnlineUser ou = (OnlineUser)Session["User"];
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            if (Request.QueryString["ConfirmNote"] != null)
            {
                this.Message.ForeColor = System.Drawing.Color.Green;
                this.Message.Text = Request.QueryString["ConfirmNote"].ToString();
            }

            if (!ou.IsPremium)
                Response.Redirect("~/GeneralMessage.aspx?MessageString=gfkkjFSDei3w7lzTD");

            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.Bookmarks_PageTitle;

            // end of security part
            ////////////////////////////////////////////////////////////////////
            if (!IsPostBack)
            {
                this.PlayersSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                this.MatchesSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                this.TeamsSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                this.UsersSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                this.CoachesSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                this.LeaguesSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
            }
        }



        protected void DeleteBM(string BookmarkId)
        {
            OnlineUser ou = (OnlineUser)Session["User"];

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_DeleteBookmark";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_BookmarkId = new SqlParameter();
            dbParam_BookmarkId.ParameterName = "@BookmarkId";
            dbParam_BookmarkId.Value = BookmarkId;
            dbParam_BookmarkId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_BookmarkId);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            SqlParameter dbParam_Language = new SqlParameter();
            dbParam_Language.ParameterName = "@Language";
            dbParam_Language.Value = ou.Language;
            dbParam_Language.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_Language);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                Response.Redirect("Bookmarks.aspx?ConfirmNote=" + dbParam_Answer.Value.ToString());
                
            }
            catch (Exception SQLDBException)
            {
                this.Message.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }

        }


        protected void DeletePlayer(Object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "DeletePlayer")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = PlayersGridView.Rows[index];
                //string ReturnValue = "";
                string BookmarkId = row.Cells[0].Text;
                DeleteBM(BookmarkId);
            }
        }


        protected void DeleteMatch(Object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "DeleteMatch")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = MatchesGridView.Rows[index];
                string BookmarkId = row.Cells[0].Text;
                //this.Message.Text = row.Cells[0].Text.ToString();
                //this.Message.Text = BookmarkId;
                DeleteBM(BookmarkId);
            }
        }

        protected void DeleteTeam(Object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "DeleteTeam")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = TeamsGridView.Rows[index];
                //string ReturnValue = "";
                string BookmarkId = row.Cells[0].Text;
                DeleteBM(BookmarkId);
            }
            
        }


        protected void DeleteCoach(Object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "DeleteCoach")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = CoachesGridView.Rows[index];
                //string ReturnValue = "";
                string BookmarkId = row.Cells[0].Text;
                //this.Message.Text = BookmarkId;
                DeleteBM(BookmarkId);
            }
        }

        protected void DeleteUser(Object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "DeleteUser")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = UsersGridView.Rows[index];
                //string ReturnValue = "";
                string BookmarkId = row.Cells[0].Text;
                //this.Message.Text = BookmarkId;
                DeleteBM(BookmarkId);
            }
            
        }

        protected void DeleteLeague(Object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "DeleteLeague")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = LeaguesGridView.Rows[index];
                //string ReturnValue = "";
                string BookmarkId = row.Cells[0].Text;
                //this.Message.Text = BookmarkId;
                DeleteBM(BookmarkId);
            }
        }


        protected void PlayersGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType !=  DataControlRowType.Pager)
            {
                e.Row.Cells[0].Visible = false;
            }
        }

        protected void MatchesGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.Pager)
            {
                e.Row.Cells[0].Visible = false;
            }

        }

        protected void TeamsGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.Pager)
            {
                e.Row.Cells[0].Visible = false;
            }

        }

        protected void UsersGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.Pager)
            {
                e.Row.Cells[0].Visible = false;
            }

        }

        protected void CoachesGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.Pager)
            {
                e.Row.Cells[0].Visible = false;
            }

        }

        protected void LeaguesGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.Pager)
            {
                e.Row.Cells[0].Visible = false;
            }

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected string DeleteBookMarkButtonOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.Bookmarks_DeleteBookMarkButton + "')");
        }
    }
}